﻿using Furion;
using Furion.Schedule;
using Furion.TimeCrontab;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OAS.Application;
using OAS.Core;
using OAS.Web.Core.Handlers;
using System;

namespace OAS.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSqlsugarSetup(App.Configuration);
        services.AddConsoleFormatter(); 
        services.AddJwt<JwtHandler>(enableGlobalAuthorize: true); 
        services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
        {
            options.MinimumLevel = LogLevel.Information;
        });
        services.AddCorsAccessor();
        services.AddSchedule(options =>
        {
            // 注册作业，并配置作业触发器
            options.AddJob<AttendanceJob>(Triggers.Cron("0 0 23 * * ?")); // 表示每天23点执行
        });
        services.AddControllers()
                .AddInjectWithUnifyResult()
                .AddNewtonsoftJson();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();
        app.UseHttpLogging();
        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
